package com.jxs.android.weixi;

import org.ksoap2.serialization.SoapObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.jxs.android.weiget.RefreshListView;
import com.telecom.weixi.R;

@SuppressLint("HandlerLeak")
public abstract class LoadingMoreAbstractList extends AbstractListActivity {

	private static final int UPDATE_LIST_UI = 0x101;
	private static final int ERROR_MSG = 0x1013;
	private static final int TO_LOGIN = 0x1019;

	protected RefreshListView refreshListView;
	protected TextView empytText;

	protected int currentPage = 1; // 最后的可视项索引
	protected View loadMoreView;
	protected Button loadMoreButton;

	protected String title;
	protected int PAGE_SIZE = 5;

	public int getLayout() {
		return R.layout.bu_common_with_list;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (null != getIntent() && null != getIntent().getExtras()) {
			title = getIntent().getExtras().getString("title");
		}
		initTopBar(savedInstanceState, getLayout());

		loadMoreView = LayoutInflater.from(this).inflate(R.layout.bu_loading_btn, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText("正在加载中..."); // 设置按钮文字
				loadingData(currentPage, true);
			}
		});

		refreshListView = (RefreshListView) findViewById(R.id.common_listview);
		addListHeadView(refreshListView);
		refreshListView.setDivider(null);
		refreshListView.setDividerHeight(5);
		refreshListView.setAdapter(getAdapter(this));
		refreshListView.addFooterView(loadMoreView);
		empytText = (TextView) findViewById(R.id.null_has_data);
		refreshListView.setonRefreshListener(new RefreshListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				if (loadMoreButton.isEnabled()) {
					loadingData(currentPage, false);
				} else {
					refreshListView.onRefreshComplete();
				}
			}
		});
		if (isOnCreatLoading()) {
			loadingData(currentPage, true);
		} else {
			loadMoreButton.setText(null);
		}

	}

	protected void addListHeadView(RefreshListView refreshListView) {
	}

	protected boolean isOnCreatLoading() {
		return true;
	}

	public abstract void loadingData(int currentPage, boolean showDialog);

	public abstract BaseAdapter getAdapter(Context context);

	public abstract Object paserData(SoapObject retMsgObject, Object... params);

	public abstract void updateList(Message msg);

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void paser(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				Bundle data = new Bundle();
				if (soapObject.hasProperty("totalRows")) {
					data.putString("totalRows", getValue(soapObject, "totalRows"));
				} else {
					data.putString("totalRows", "");
				}
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.setData(data);
				msg.obj = paserData(taskBeanObject, params);
				handler.sendMessage(msg);
			}
		}
	}

	public String getValue(SoapObject taskBeanObject, String key) {
		if (null != taskBeanObject.getProperty(key)) {
			return taskBeanObject.getProperty(key).toString();
		}
		return "";
	}

	Handler handler = new Handler() {

		@SuppressLint({ "HandlerLeak", "HandlerLeak" })
		@Override
		public void handleMessage(Message msg) {

			refreshListView.onRefreshComplete();
			if (msg.what == UPDATE_LIST_UI) {
				updateList(msg);
			} else if (msg.what == ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			}
		}

	};

}
